home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / multiprocessing / util.pyc (.txt) < prev   
Python Compiled Bytecode  |  2009-11-11  |  10KB  |  307 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import itertools
  5. import weakref
  6. import atexit
  7. import threading
  8. from multiprocessing.process import current_process, active_children
  9. __all__ = [
  10.     'sub_debug',
  11.     'debug',
  12.     'info',
  13.     'sub_warning',
  14.     'get_logger',
  15.     'log_to_stderr',
  16.     'get_temp_dir',
  17.     'register_after_fork',
  18.     'is_exiting',
  19.     'Finalize',
  20.     'ForkAwareThreadLock',
  21.     'ForkAwareLocal',
  22.     'SUBDEBUG',
  23.     'SUBWARNING']
  24. NOTSET = 0
  25. SUBDEBUG = 5
  26. DEBUG = 10
  27. INFO = 20
  28. SUBWARNING = 25
  29. LOGGER_NAME = 'multiprocessing'
  30. DEFAULT_LOGGING_FORMAT = '[%(levelname)s/%(processName)s] %(message)s'
  31. _logger = None
  32. _log_to_stderr = False
  33.  
  34. def sub_debug(msg, *args):
  35.     if _logger:
  36.         _logger.log(SUBDEBUG, msg, *args)
  37.     
  38.  
  39.  
  40. def debug(msg, *args):
  41.     if _logger:
  42.         _logger.log(DEBUG, msg, *args)
  43.     
  44.  
  45.  
  46. def info(msg, *args):
  47.     if _logger:
  48.         _logger.log(INFO, msg, *args)
  49.     
  50.  
  51.  
  52. def sub_warning(msg, *args):
  53.     if _logger:
  54.         _logger.log(SUBWARNING, msg, *args)
  55.     
  56.  
  57.  
  58. def get_logger():
  59.     '''
  60.     Returns logger used by multiprocessing
  61.     '''
  62.     global _logger
  63.     import logging as logging
  64.     import atexit as atexit
  65.     logging._acquireLock()
  66.     
  67.     try:
  68.         if not _logger:
  69.             _logger = logging.getLogger(LOGGER_NAME)
  70.             _logger.propagate = 0
  71.             logging.addLevelName(SUBDEBUG, 'SUBDEBUG')
  72.             logging.addLevelName(SUBWARNING, 'SUBWARNING')
  73.             if hasattr(atexit, 'unregister'):
  74.                 atexit.unregister(_exit_function)
  75.                 atexit.register(_exit_function)
  76.             else:
  77.                 atexit._exithandlers.remove((_exit_function, (), { }))
  78.                 atexit._exithandlers.append((_exit_function, (), { }))
  79.     finally:
  80.         logging._releaseLock()
  81.  
  82.     return _logger
  83.  
  84.  
  85. def log_to_stderr(level = None):
  86.     '''
  87.     Turn on logging and add a handler which prints to stderr
  88.     '''
  89.     global _log_to_stderr
  90.     import logging
  91.     logger = get_logger()
  92.     formatter = logging.Formatter(DEFAULT_LOGGING_FORMAT)
  93.     handler = logging.StreamHandler()
  94.     handler.setFormatter(formatter)
  95.     logger.addHandler(handler)
  96.     if level:
  97.         logger.setLevel(level)
  98.     
  99.     _log_to_stderr = True
  100.     return _logger
  101.  
  102.  
  103. def get_temp_dir():
  104.     if current_process()._tempdir is None:
  105.         import shutil as shutil
  106.         import tempfile as tempfile
  107.         tempdir = tempfile.mkdtemp(prefix = 'pymp-')
  108.         info('created temp directory %s', tempdir)
  109.         Finalize(None, shutil.rmtree, args = [
  110.             tempdir], exitpriority = -100)
  111.         current_process()._tempdir = tempdir
  112.     
  113.     return current_process()._tempdir
  114.  
  115. _afterfork_registry = weakref.WeakValueDictionary()
  116. _afterfork_counter = itertools.count()
  117.  
  118. def _run_after_forkers():
  119.     items = list(_afterfork_registry.items())
  120.     items.sort()
  121.     for index, ident, func in items:
  122.         obj = None
  123.         
  124.         try:
  125.             func(obj)
  126.         continue
  127.         except Exception:
  128.             e = None
  129.             info('after forker raised exception %s', e)
  130.             continue
  131.         
  132.  
  133.     
  134.  
  135.  
  136. def register_after_fork(obj, func):
  137.     _afterfork_registry[(_afterfork_counter.next(), id(obj), func)] = obj
  138.  
  139. _finalizer_registry = { }
  140. _finalizer_counter = itertools.count()
  141.  
  142. class Finalize(object):
  143.     '''
  144.     Class which supports object finalization using weakrefs
  145.     '''
  146.     
  147.     def __init__(self, obj, callback, args = (), kwargs = None, exitpriority = None):
  148.         if not exitpriority is None and type(exitpriority) is int:
  149.             raise AssertionError
  150.         if obj is not None:
  151.             self._weakref = weakref.ref(obj, self)
  152.         elif not exitpriority is not None:
  153.             raise AssertionError
  154.         self._callback = callback
  155.         self._args = args
  156.         if not kwargs:
  157.             pass
  158.         self._kwargs = { }
  159.         self._key = (exitpriority, _finalizer_counter.next())
  160.         _finalizer_registry[self._key] = self
  161.  
  162.     
  163.     def __call__(self, wr = None):
  164.         '''
  165.         Run the callback unless it has already been called or cancelled
  166.         '''
  167.         
  168.         try:
  169.             del _finalizer_registry[self._key]
  170.         except KeyError:
  171.             sub_debug('finalizer no longer registered')
  172.  
  173.         sub_debug('finalizer calling %s with args %s and kwargs %s', self._callback, self._args, self._kwargs)
  174.         res = self._callback(*self._args, **self._kwargs)
  175.         self._weakref = None
  176.         self._callback = None
  177.         self._args = None
  178.         self._kwargs = None
  179.         self._key = None
  180.         return res
  181.  
  182.     
  183.     def cancel(self):
  184.         '''
  185.         Cancel finalization of the object
  186.         '''
  187.         
  188.         try:
  189.             del _finalizer_registry[self._key]
  190.         except KeyError:
  191.             pass
  192.  
  193.         self._weakref = None
  194.         self._callback = None
  195.         self._args = None
  196.         self._kwargs = None
  197.         self._key = None
  198.  
  199.     
  200.     def still_active(self):
  201.         '''
  202.         Return whether this finalizer is still waiting to invoke callback
  203.         '''
  204.         return self._key in _finalizer_registry
  205.  
  206.     
  207.     def __repr__(self):
  208.         
  209.         try:
  210.             obj = self._weakref()
  211.         except (AttributeError, TypeError):
  212.             obj = None
  213.  
  214.         if obj is None:
  215.             return '<Finalize object, dead>'
  216.         x = '<Finalize object, callback=%s' % getattr(self._callback, '__name__', self._callback)
  217.         if self._args:
  218.             x += ', args=' + str(self._args)
  219.         
  220.         if self._kwargs:
  221.             x += ', kwargs=' + str(self._kwargs)
  222.         
  223.         if self._key[0] is not None:
  224.             x += ', exitprority=' + str(self._key[0])
  225.         
  226.         return x + '>'
  227.  
  228.  
  229.  
  230. def _run_finalizers(minpriority = None):
  231.     '''
  232.     Run all finalizers whose exit priority is not None and at least minpriority
  233.  
  234.     Finalizers with highest priority are called first; finalizers with
  235.     the same priority will be called in reverse order of creation.
  236.     '''
  237.     items = _[1]
  238.     items.sort(reverse = True)
  239.     for key, finalizer in items:
  240.         sub_debug('calling %s', finalizer)
  241.         
  242.         try:
  243.             finalizer()
  244.         continue
  245.         except Exception:
  246.             []
  247.             []
  248.             []
  249.             import traceback as traceback
  250.             traceback.print_exc()
  251.             continue
  252.             None if minpriority is None else (None,)
  253.         
  254.  
  255.     
  256.  
  257.  
  258. def is_exiting():
  259.     '''
  260.     Returns true if the process is shutting down
  261.     '''
  262.     if not _exiting:
  263.         pass
  264.     return _exiting is None
  265.  
  266. _exiting = False
  267.  
  268. def _exit_function():
  269.     info('process shutting down')
  270.     debug('running all "atexit" finalizers with priority >= 0')
  271.     _run_finalizers(0)
  272.     for p in active_children():
  273.         if p._daemonic:
  274.             info('calling terminate() for daemon %s', p.name)
  275.             p._popen.terminate()
  276.             continue
  277.     
  278.     for p in active_children():
  279.         info('calling join() for process %s', p.name)
  280.         p.join()
  281.     
  282.     debug('running the remaining "atexit" finalizers')
  283.     _run_finalizers()
  284.  
  285. atexit.register(_exit_function)
  286.  
  287. class ForkAwareThreadLock(object):
  288.     
  289.     def __init__(self):
  290.         self._lock = threading.Lock()
  291.         self.acquire = self._lock.acquire
  292.         self.release = self._lock.release
  293.         register_after_fork(self, ForkAwareThreadLock.__init__)
  294.  
  295.  
  296.  
  297. class ForkAwareLocal(threading.local):
  298.     
  299.     def __init__(self):
  300.         register_after_fork(self, (lambda obj: obj.__dict__.clear()))
  301.  
  302.     
  303.     def __reduce__(self):
  304.         return (type(self), ())
  305.  
  306.  
  307.